package rock.remoting.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import rock.common.enums.RpcResponseCodeEnum;

import java.io.Serializable;

@AllArgsConstructor
@NoArgsConstructor
@Getter@Setter
@Builder
@ToString
public class RpcResponse<T> implements Serializable {

    private static final long serialVersionUID = -804338883750048659L;

    private String requestId;
    /**
     * 响应code
     */
    private Integer code;
    /**
     * 响应额外信息
     */
    private String message;
    /**
     * 响应包体
     */
    private T data;

    /**
     * 成功返回
     * @param data 数据
     * @param requestId 请求id
     * @param <T> 数据类型
     * @return <T>data
     */
    public static <T> RpcResponse<T> success(T data, String requestId) {
        RpcResponse<T> response = new RpcResponse<T>();
        response.setCode(RpcResponseCodeEnum.SUCCESS.getCode());
        response.setMessage(RpcResponseCodeEnum.SUCCESS.getMessage());

        response.setRequestId(requestId);

        if (null != data) { response.setData(data); }

        return response;
    }

    /**
     * 失败返回
     * @param rpcResponseCodeEnum 失败枚举
     * @param <T> 类型
     * @return
     */
    public static <T> RpcResponse<T> fail(RpcResponseCodeEnum rpcResponseCodeEnum) {
        RpcResponse<T> response = new RpcResponse<T>();
        response.setCode(rpcResponseCodeEnum.getCode());
        response.setMessage(rpcResponseCodeEnum.getMessage());
        return response;
    }
}
